package net.lab1024.sa.admin.module.sast.project.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.constant.SastConst;
import net.lab1024.sa.admin.module.business.category.constant.CategoryTypeEnum;
import net.lab1024.sa.admin.module.business.category.domain.entity.CategoryEntity;
import net.lab1024.sa.admin.module.sast.project.dao.ProjectDao;
import net.lab1024.sa.admin.module.sast.project.domain.*;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartEnumUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.module.support.file.constant.FileFolderTypeEnum;
import net.lab1024.sa.base.module.support.file.dao.FileDao;
import net.lab1024.sa.base.module.support.file.service.IFileStorageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Optional;


@Service
public class ProjectService {


    private static final int FILE_NAME_MAX_LENGTH = 100;
    @Resource
    private IFileStorageService fileStorageService;

    @Resource
    private ProjectUploadService projectUploadService;
    @Resource
    private FileDao fileDao;
    @Resource
    private ProjectDao projectDao;

    /**
     * 分页查询项目信息
     * @param queryForm
     * @return
     */
    public ResponseDTO<PageResult<ProjectVO>> queryProjects(ProjectQueryForm queryForm){
        Page<?> page= SmartPageUtil.convert2PageQuery(queryForm);
        List<ProjectVO> list= projectDao.query(page,queryForm);
        PageResult<ProjectVO> pageResult= SmartPageUtil.convert2PageResult(page,list);
        return ResponseDTO.ok(pageResult);
    }
    public ResponseDTO<?> getProjectCount() {
        Long count = projectDao.selectCount(null);
        return ResponseDTO.ok(count);
    }
    public ResponseDTO<ProjectUploadVO> projectZipUpload(MultipartFile file, Integer folderType, RequestUser requestUser) {
        FileFolderTypeEnum folderTypeEnum = SmartEnumUtil.getEnumByValue(folderType, FileFolderTypeEnum.class);
        if (null == folderTypeEnum) {
            return ResponseDTO.userErrorParam("文件夹错误");
        }

        if (null == file || file.getSize() == 0) {
            return ResponseDTO.userErrorParam("上传文件不能为空");
        }

        // 校验文件名称
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            return ResponseDTO.userErrorParam("上传文件名称不能为空");
        }

        if (originalFilename.length() > FILE_NAME_MAX_LENGTH) {
            return ResponseDTO.userErrorParam("文件名称最大长度为：" + FILE_NAME_MAX_LENGTH);
        }

        //todo 自定义的对多个文件的校验
        // 校验文件大小以及安全性
//        ResponseDTO<String> validateFile = securityFileService.checkFile(file);
//        if (!validateFile.getOk()) {
//            return ResponseDTO.error(validateFile);
//        }

        //todo 解压
        projectUploadService.uploadAndUnzipProjectFile(file);
        // 进行上传
        return null;

    }

    public void createProject(ProjectForm projectForm) {
        ProjectEntity projectEntity = SmartBeanUtil.copy(projectForm,ProjectEntity.class);
        projectEntity.setLongName(projectForm.getName());
        projectEntity.setBranchType(SastConst.PROJECT_BRANCH_TYPE_LONG);
        projectEntity.setQualifier(SastConst.PROJECT_QUALIFIER_TRK);
        projectDao.createProject(projectEntity);
    }

    /**
     * 校验项目uuid有效性
     * @param uuid
     * @return
     */
    public Optional<ProjectEntity> queryProjectByUuId(String uuid) {
        if (null == uuid) {
            return Optional.empty();
        }
        ProjectEntity result = projectDao.selectOne(new QueryWrapper<ProjectEntity>().eq("uuid", uuid));
        if (null == result) {
            return Optional.empty();
        }
        return Optional.of(result);
    }
}
